// pages/collect/collect.js
import { Collect } from '../../../models/collect'
import {check} from '../../../util/identityCheck'

const collectModel = new Collect()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: false,
    data: [],
    tagColors: {
      '招聘': '#4C94FF',
      '买店': '#FF8C5B',
      '卖店': '#8E6FFF',
      '供应商': '#1BCFCB',
      '招标': '#FF5871'
    },
    page: 1
  },

  onShow () {
    if(!check())return
  },

  onLoad (options) {
    this.getCollect()
  },

  getCollect (type) {
  
    collectModel.collectList({
      page: this.data.page
    }, res => {
      this.setData({
        data: type == 'more' ? [...this.data.data, ...res] : res,
        showLoading: res.length >= 10
      })
    })
  },

  cancelCollect (event) {
    let index = event.currentTarget.dataset.index
    let data = this.data.data
    collectModel.cancelCollect({
      collectId: data[index].collectId,
      collectType: data[index].collectType
    })
    data.splice(index, 1)
    this.setData({
      data
    })
  },

  onClose (event) {
  },

  toNewPage (event) {
    let {id, type} = event.currentTarget.dataset
    switch (type) {
      case '招标': 
      wx.navigateTo({
        url: `/pages/bidding-detail/bidding-detail?id=${id}`
      })
        break
      case '买店':
        wx.navigateTo({
          url: `/pages/buyshop-detail/buyshop-detail?id=${id}`
        })
        break
      case '卖店':
        wx.navigateTo({
          url: `/pages/sellshop-detail/sellshop-detail?id=${id}`
        })
        break
      case '供应商':
        wx.navigateTo({
          url: `/pages/supply-detail/supply-detail?id=${id}`
        })
        break
      case '招聘':
        wx.navigateTo({
          url: `/pages/recruit-detail/recruit-detail?id=${id}`
        })
        break
    }
  },

  onReachBottom () {
    this.setData({
      page: this.data.page + 1
    })
    this.getCollect('more')
  },

})
